package com.softpo.pointersgesture.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Created by softpo on 2016/11/23.
 */

public class MoveImage extends ImageView {

    private int mScreenWidth;
    private int mScreenHeight;

    public MoveImage(Context context) {
        super(context,null);
    }

    public MoveImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        mScreenWidth = displayMetrics.widthPixels;
        mScreenHeight = displayMetrics.heightPixels;
    }

    //作为记录，记录是上一次的坐标的位置
    private float last_x =0;
    private float last_y =0;
    //移动自定义控件
    public void moveImage(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                //获取移动当前坐标
                //永远时最新的坐标
                float x = event.getX();
                float y = event.getY();

                //从A点（之前）移动到B点（现在）
                if(x-last_x>10||y-last_y>10||x-last_x<-10||y-last_y<-10){

                    if(x-last_x>30||y-last_y>30||x-last_x<-30||y-last_y<-30){
                        //不允许图片移动，不允许图片大幅度滑动
                    }else {
                        updateLoc(x-last_x,y-last_y);
                    }
                    //重新赋一个值
                    last_x = event.getX();
                    last_y = event.getY();
                }
                break;
        }
    }

    //更新图片的未知
    private void updateLoc(float x, float y) {
        int offsetX = (int) x;
        int offsetY = (int) y;
        //将图片重新布局
        this.setFrame(
                getLeft()+offsetX,
                getTop()+offsetY,
                getRight()+offsetX,
                getBottom()+offsetY);
    }

    //缩放比率
    private float scaleSize =  0.05f;
    //记录两点之间上一次的间距
    private int  last_gap = 0;
    //缩放图片
    public void scaleImage(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                //获取第一个点x y坐标
                float x1 = event.getX(0);
                float y1 = event.getY(0);

                //获取第二个点x y 坐标
                float x2 = event.getX(1);
                float y2 = event.getY(1);

                float gapX = x1 - x2;
                float gapY = y1 - y2;

                int gap = (int) Math.sqrt(gapX * gapX + gapY * gapY);

                if(gap-last_gap>20||gap-last_gap<-20){
                    if(gap-last_gap>40||gap-last_gap<-40){

                    }else {
                        if(gap-last_gap>20){//放大
                            scale(1);
                        }else if(gap - last_gap< -20 ){//缩小
                            scale(0);
                        }
                    }
                    //该次的两点之间的间距，变成上一次的间距
                    last_gap = gap;
                }


                break;
        }
    }

    private void scale(int flag) {
        int scaleWidth = (int) (getWidth() * scaleSize);
        int scaleHeight = (int) (getHeight() * scaleSize);
        switch (flag){
            case 0://缩小，其小有形
                if(getWidth()>50){
                    this.setFrame(
                            getLeft()+ scaleWidth,
                            getTop()+ scaleHeight,
                            getRight()-scaleWidth,
                            getBottom()-scaleHeight);
                }
                break;
            case 1://放大,其大有边
                if(getWidth()<mScreenWidth*3&&getHeight()<mScreenHeight*3){
                    this.setFrame(
                            getLeft()- scaleWidth,
                            getTop()- scaleHeight,
                            getRight()+scaleWidth,
                            getBottom()+scaleHeight);
                }
                break;
        }
    }
}
